package fn;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;

import org.scilab.forge.jlatexmath.TeXConstants;
import org.scilab.forge.jlatexmath.TeXFormula;
import org.scilab.forge.jlatexmath.TeXIcon;

public class Latex {

	public static TeXIcon create(Function f, int s) {
		try {
			TeXIcon ti = new TeXFormula(f.toLatex()).createTeXIcon(TeXConstants.STYLE_DISPLAY, s);
			ti.isColored = true;
			return ti;
		} catch (Exception e) {}
		return new TeXFormula("error").createTeXIcon(TeXConstants.STYLE_DISPLAY, s);
	}

	public static TeXIcon create(String f, int s) {
		TeXIcon ti = new TeXFormula(f).createTeXIcon(TeXConstants.STYLE_DISPLAY, s);
		ti.isColored = true;
		return ti;
	}

	public static void draw(Function f, int s, Component c, Graphics g, Color col) {
		TeXIcon ti = new TeXFormula(f.toLatex()).createTeXIcon(TeXConstants.STYLE_DISPLAY, s);
		ti.isColored = true;
		ti.setForeground(col);
		ti.paintIcon(c, g, 0, 0);
	}

	public static void draw(String f, int s, Component c, Graphics g, Color col) {
		TeXIcon ti = new TeXFormula(f).createTeXIcon(TeXConstants.STYLE_DISPLAY, s);
		ti.isColored = true;
		ti.setForeground(col);
		ti.paintIcon(c, g, 0, 0);
	}

	public static void show(String s) {
		final TeXIcon ti = new TeXFormula(s).createTeXIcon(TeXConstants.STYLE_DISPLAY, 16);
		ti.isColored = true;
		ti.setForeground(Color.black);
		JFrame f = new JFrame();
		f.setContentPane(new JPanel() {
			private static final long serialVersionUID = -1080008641750266343L;

			@Override
			public void paint(Graphics g) {
				super.paint(g);
				ti.paintIcon(this, g, 0, 0);
				getParent().setSize(ti.getIconWidth(), ti.getIconHeight());
				getParent().setPreferredSize(getSize());
			}
		});
		f.setVisible(true);
	}
}
